package cn.edu.cdp.dumpling.service.impl;

import cn.edu.cdp.dumpling.entity.Account;
import cn.edu.cdp.dumpling.entity.Consumer;
import cn.edu.cdp.dumpling.mapper.AccountMapper;
import cn.edu.cdp.dumpling.mapper.ConsumerMapper;
import cn.edu.cdp.dumpling.service.ConsumerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer> implements ConsumerService {

    private final AccountMapper accountMapper;

    public ConsumerServiceImpl(AccountMapper accountMapper) {
        this.accountMapper = accountMapper;
    }

    @Override
    public Consumer login(Account account) {
        LambdaQueryWrapper<Account> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Account::getAccount, account.getAccount());
        wrapper1.eq(Account::getPassword, account.getPassword());

        Account a = accountMapper.selectOne(wrapper1);
        if (a == null) {
            return null;
        } else {
            LambdaQueryWrapper<Consumer> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(Consumer::getTel, a.getAccount());   // 电话号码就是账号
            return this.getOne(wrapper2);
        }
    }
}
